package controller
{
	import away3d.animators.SkeletonAnimator;
	import away3d.animators.transitions.CrossfadeTransition;
	import away3d.events.MouseEvent3D;
	
	import flash.events.MouseEvent;
	
	import gameObject.GameObjectBase;
	import gameObject.RobotPart;
	import gameObject.resources3D.RobotResource;
	
	import tool.tweener.Tweener;

	public class TestActionController extends GameControllerBase
	{
		private var _robotPart:RobotPart;
		private var _robotRes:RobotResource;
		
		private var _bodyRotateMark:Number;
		private var _legRotateMark:Number;
		private var _mouseMark:Number;
		
		private var _isRun:Boolean = false;
		
		private var animator:SkeletonAnimator;
		private var transition:CrossfadeTransition = new CrossfadeTransition(1);
		/////////////////////////////////////////////////////
		public function TestActionController(){}
		
		override public function setup(target:GameObjectBase):void
		{
			super.setup(target);
			
			_robotPart = target as RobotPart;
			_robotRes = _robotPart.resource as RobotResource;
			
			_robotRes.bodyMesh.mouseEnabled = true;
			_robotRes.bodyMesh.addEventListener(MouseEvent3D.MOUSE_DOWN,onBodyMouseDown);
			_robotRes.bodyMesh.addEventListener(MouseEvent3D.DOUBLE_CLICK,onDoubleClick);
			
			_robotRes.legMesh.mouseEnabled = true;
			_robotRes.legMesh.addEventListener(MouseEvent3D.MOUSE_DOWN,onLegMouseDown);
			_robotRes.legMesh.addEventListener(MouseEvent3D.DOUBLE_CLICK,onDoubleClick);
			
			animator = _robotRes.legMesh.animator as SkeletonAnimator;
			animator.play("wait",transition);
		}
		
		override public function dispose():void
		{
			_robotRes.bodyMesh.removeEventListener(MouseEvent3D.MOUSE_DOWN,onBodyMouseDown);
			_robotRes.bodyMesh.removeEventListener(MouseEvent3D.DOUBLE_CLICK,onDoubleClick);
			_robotRes.legMesh.removeEventListener(MouseEvent3D.MOUSE_DOWN,onLegMouseDown);
			_robotRes.legMesh.removeEventListener(MouseEvent3D.DOUBLE_CLICK,onDoubleClick);
			
			Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onBodyMouseMove);
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP,onBodyMouseUp);
			Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onLegMouseMove);
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP,onLegMouseUp);
			
			_target.controller = null;
		}
		
		public function stop():void
		{
			animator.stop();
		}
		
		//////////////////////////////////////////////////////////////////
		private function onBodyMouseDown(e:MouseEvent3D):void
		{
			_bodyRotateMark = _robotRes.connector.rotationZ;
			_mouseMark = Global.stage.mouseX;
			
			Global.stage.addEventListener(MouseEvent.MOUSE_MOVE,onBodyMouseMove);
			Global.stage.addEventListener(MouseEvent.MOUSE_UP,onBodyMouseUp);
		}
		
		private function onLegMouseDown(e:MouseEvent3D):void
		{
			_legRotateMark = _robotRes.rotationY;
			_mouseMark = Global.stage.mouseX;
			
			Global.stage.addEventListener(MouseEvent.MOUSE_MOVE,onLegMouseMove);
			Global.stage.addEventListener(MouseEvent.MOUSE_UP,onLegMouseUp);
		}
		
		private function onBodyMouseMove(e:MouseEvent):void
		{
			var robVal:Number = _bodyRotateMark + 0.4*(Global.stage.mouseX-_mouseMark);
			
			if(robVal>210)robVal = 210;
			if(robVal<-30)robVal = -30;
			
			_robotRes.connector.rotationZ = robVal;
		}
		
		private function onLegMouseMove(e:MouseEvent):void
		{
			_robotRes.rotationY = _legRotateMark + 0.4*(_mouseMark-Global.stage.mouseX);
		}
		
		private function onBodyMouseUp(e:MouseEvent):void
		{
			Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onBodyMouseMove);
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP,onBodyMouseUp);
		}
		
		private function onLegMouseUp(e:MouseEvent):void
		{
			Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onLegMouseMove);
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP,onLegMouseUp);
		}
		
		private function onDoubleClick(e:MouseEvent3D):void
		{
			_isRun = !_isRun;
			if(_isRun)animator.play("run",transition)
			else
			{
				_robotRes.bodyMesh.removeEventListener(MouseEvent3D.MOUSE_DOWN,onBodyMouseDown);
				Tweener.addTween(_robotRes.connector,{rotationZ:90,time:1,transition:"easeinoutsine",onComplete:gotPosition});
				animator.play("wait",transition);
			}
		}
		private function gotPosition():void
		{
			_robotRes.bodyMesh.addEventListener(MouseEvent3D.MOUSE_DOWN,onBodyMouseDown);
		}
	}
}